/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.controller;

import edu.upc.clase.demo.entity.CriterioBusquedaCancha;
import edu.upc.clase.demo.entity.Cancha;
import edu.upc.clase.demo.service.CanchaService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;


/**
 *
 * @author Hariki
 */
@Controller("canchaController")
public class CanchaController {
   
    @Autowired
    private CanchaService canchaService;
    private static Logger log = LoggerFactory.getLogger(CanchaController.class);

    @RequestMapping("/cancha/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("cancha/index");
        List<Cancha> canchas = canchaService.buscarTodos();
        CriterioBusquedaCancha criterioBusquedaCancha = new CriterioBusquedaCancha();
        mav.addObject("cancha", canchas);
        mav.addObject("criterioBusquedaCancha",criterioBusquedaCancha);
        return mav;
    }
    
    @RequestMapping(value = "/cancha/new", method = RequestMethod.GET)
    public ModelAndView newCancha() {
        ModelAndView mav = new ModelAndView("cancha/new");
        Cancha cancha = new Cancha();
        mav.getModelMap().put("cancha", cancha);
        return mav;
    }

    @RequestMapping(value = "/cancha/new", method = RequestMethod.POST)
    public String createCancha(@ModelAttribute("cancha")Cancha cancha, SessionStatus status) {    
        canchaService.insertar(cancha);
        status.setComplete();

        return "redirect:/pages/cancha/index";
        
    }
    
    @RequestMapping(value = "/cancha/edit", method = RequestMethod.GET)
    public ModelAndView editUsuario(@RequestParam("CoCancha")Integer CoCancha) {    
        ModelAndView mav = new ModelAndView("cancha/edit");
        Cancha cancha = canchaService.buscar(CoCancha);
        mav.getModelMap().put("cancha", cancha);
        return mav;        
    }
    
    @RequestMapping(value="/cancha/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("cancha") Cancha cancha, SessionStatus status) {
        canchaService.actualizar(cancha);
        status.setComplete();
        return "redirect:/pages/cancha/index";
    }
    
    @RequestMapping("/cancha/delete")
    public ModelAndView delete(@RequestParam("CoCancha")Integer CoCancha)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/cancha/index");
        Cancha cancha = canchaService.buscar(CoCancha);
        canchaService.eliminar(cancha);
        return mav;
    }
            
    @RequestMapping(value = "/cancha/buscar", method=RequestMethod.POST)
    public ModelAndView buscar(@ModelAttribute("criterioBusquedaCancha") CriterioBusquedaCancha criterio, SessionStatus status) {
        ModelAndView mav = new ModelAndView("cancha/resultadoBusqueda");
        log.info("resultado = " + canchaService.buscarPorNombre(criterio.getNoCancha()).size());
        mav.getModel().put("resultado",canchaService.buscarPorNombre(criterio.getNoCancha()));
        return mav;
    }
    
    
    
}
